package com.vj.analyzeFakeNames.rawData;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import junit.framework.Assert;

import org.junit.Test;

/**
 * process data ( *.txt ) to Properties entry. ( key = value )
 * 
 */
public class RawDataProcessor {

	@Test
	public void transform() throws IOException {
		String fileName = "b.txt";
		int lines = transform(fileName);
		
		fileName = "c.txt";
		lines = transform(fileName);
		
		fileName = "d.txt";
		lines = transform(fileName);

		fileName = "e.txt";
		lines = transform(fileName);
		
		Assert.assertTrue("Wrong", lines > 0);
	}

	private int transform(String fileName) throws IOException {
		// TODO Auto-generated method stub
		fileName = "src/com/vj/analyzeFakeNames/rawData/" + fileName;
//		System.out.println(new File(fileName).getAbsolutePath());
		BufferedReader br = new BufferedReader(new FileReader(fileName));
		String line = br.readLine();
		int tag = 0; // tag 1: name; tag 2+ tag 3: address;
		StringBuffer buffer = new StringBuffer();
		String tmpLine = null;
		boolean right = false;
		while( line != null){
			line = line.trim();
			if(line.length() > 0) {
				tag ++;
			}
			if( 1 == tag) {//Bo Chuang
				buffer.append("name="+line).append("\n");
			} else if (2==tag){// 4448 Austin Avenue
				tmpLine = line;
			} else if( 3==tag && line.length()>0){// Waycross, GA 31501
				buffer.append("address="+tmpLine+" "+line).append("\n");
				tmpLine=null;
			} else if(4==tag){//Curious what Bo means? Click here to find out!
				continue;
			} else if(line.startsWith("Phone")){
				right = true;
				tmpLine="Phone";
			} else if(line.startsWith("Email Address")){
				right = true; tmpLine="EmailAddress";
			} else if(line.startsWith("Username")){
				right = true; tmpLine="Username";						
			} else if(line.startsWith("Password")){
				right = true; tmpLine="Password";						
			} else if(line.startsWith("Birthday")){
				right = true; tmpLine="Birthday";						
			} else if(line.startsWith("MasterCard") || line.startsWith("Visa")){
				right = true; tmpLine="MasterCard";						
			} else if(line.startsWith("Expires")){
				right = true; tmpLine="Expires";						
			} else if(line.startsWith("CVC2")){
				right = true; tmpLine="CVC2";						
			} else if(line.startsWith("SSN")){
				right = true; tmpLine="SSN";						
			} else if(line.startsWith("Occupation")){
				right = true; tmpLine="Occupation";						
			} else if(line.startsWith("Company")){
				right = true; tmpLine="Company";						
			} else if(line.startsWith("Website")){
				right = true; tmpLine="Website";						
			} else if(line.startsWith("Vehicle")){
				right = true; tmpLine="Vehicle";						
			} else if(line.startsWith("UPS Tracking Number")){
				right = true; tmpLine="UPSTrackingNumber";						
			} else if(line.startsWith("Blood type")){
				right = true; tmpLine="Bloodtype";						
			} else if(line.startsWith("Weight")){
				right = true; tmpLine="Weight";						
			} else if(line.startsWith("Height")){
				right = true; tmpLine="Height";						
			} else if(line.startsWith("GUID")){
				right = true; tmpLine="GUID";						
			} else if(line.startsWith("Geo coordinates")){
				right = true; tmpLine="Geocoordinates";						
			} else if(right){// key found
				buffer.append(tmpLine+"="+line).append("\n");
				right = false;
			} 
			System.out.println(line);
			
			line = br.readLine();
		}
		System.out.println("================================================");
		System.out.println(buffer);
		outputFile(buffer, fileName+".transform.properties");
		return 0;
	}

	private void outputFile(StringBuffer buffer, String fileName) throws IOException {
		// TODO Auto-generated method stub
		FileWriter write = new FileWriter(fileName);
		write.write(buffer.toString());
		write.flush();
		write.close();
	}

	public static void main(String[] args) {
	}
}
